package cn.manyufun.myf.ui.main.fragment;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.manyufun.myf.R;
import cn.manyufun.myf.bean.UserDetail;
import cn.manyufun.myf.common.base.BaseFragment;
import cn.manyufun.myf.common.commonutils.ImageLoaderUtils;
import cn.manyufun.myf.common.commonutils.UserInfoUtils;
import cn.manyufun.myf.common.commonwidget.NormalTitleBar;
import cn.manyufun.myf.ui.user.activity.LoginAct;
import cn.manyufun.myf.ui.user.activity.SettingActivity;
import cn.manyufun.myf.ui.user.model.UserInfoModel;
import cn.manyufun.myf.ui.user.presenter.UserInfoPresenter;

/**
 * @author Kevin Xiao
 * @date Create Date: 2017-12-26
 * @Description
 */
public class UserInfoFragment extends BaseFragment<UserInfoPresenter, UserInfoModel> {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.tv_col_count)
    TextView colCount;
    @BindView(R.id.tv_short_comment_count)
    TextView shortCommentCount;
    @BindView(R.id.tv_film_comment_count)
    TextView filmCommentCount;
    @BindView(R.id.tv_user_name)
    TextView userName;
    @BindView(R.id.rt_user_data)
    View userData;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @BindView(R.id.iv_user_logo)
    ImageView userLogo;

    @OnClick(R.id.ll_setting)
    public void goSetting() {
        SettingActivity.startAction(getContext());
    }


    @OnClick(R.id.btn_login)
    void submit() {
        LoginAct.startAction(getContext());
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.app_bar_myinfo;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        ntb.setTvLeftVisiable(false);
        ntb.setTitleText(getString(R.string.myinfo_title));

        mPresenter.loadUserDetailRequest();

    }

    public void returnUserDetail(UserDetail userDetail) {
        if (userDetail.UserInfo != null) {
            ImageLoaderUtils.displayRound(getContext(), userLogo, userDetail.UserInfo.HeadImgUrl);
            userData.setVisibility(View.VISIBLE);
            btnLogin.setVisibility(View.GONE);
            userName.setVisibility(View.VISIBLE);
            userName.setText(userDetail.UserInfo.NickName);
            colCount.setText(userDetail.FollowNum + "");
            shortCommentCount.setText(userDetail.CommentNum + "");
            filmCommentCount.setText(userDetail.ArticleNum + "");

            UserInfoUtils.getInstance().modify(userDetail.UserInfo);
        } else {
            ImageLoaderUtils.displayRound(getContext(), userLogo, R.drawable.ic_no_login);
            userData.setVisibility(View.GONE);
            btnLogin.setVisibility(View.VISIBLE);
            userName.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.loadUserDetailRequest();
    }
}
